package com.starsea.book.ui.newpage.normalview.delegate

import android.annotation.TargetApi
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.Gravity
import android.widget.TextView
import com.starsea.book.R
import com.starsea.book.ui.newpage.utils.DrawableUtil

/**
 * Function: TextView特有代理类
 * Description:
 */
open class RadiusTextViewDelegate(view: TextView?, context: Context?, attrs: AttributeSet?) :
  RadiusViewDelegate(
    view!!, context!!, attrs
  ) {
  private var mTextView: TextView? = null
  private var mTextColor = 0
  private var mTextPressedColor = 0
  private var mTextDisabledColor = 0
  private var mTextSelectedColor = 0
  private var mTextCheckedColor = 0
  private var mLeftDrawableColorRadius = 0f
  private var mLeftDrawableColorCircleEnable = false
  private var mLeftDrawableWidth = 0
  private var mLeftDrawableHeight = 0
  private var mLeftDrawable: Drawable? = null
  private var mLeftPressedDrawable: Drawable? = null
  private var mLeftDisabledDrawable: Drawable? = null
  private var mLeftSelectedDrawable: Drawable? = null
  private var mLeftCheckedDrawable: Drawable? = null
  private var mTopDrawableColorRadius = 0f
  private var mTopDrawableColorCircleEnable = false
  private var mTopDrawableWidth = 0
  private var mTopDrawableHeight = 0
  private var mTopDrawable: Drawable? = null
  private var mTopPressedDrawable: Drawable? = null
  private var mTopDisabledDrawable: Drawable? = null
  private var mTopSelectedDrawable: Drawable? = null
  private var mTopCheckedDrawable: Drawable? = null
  private var mRightDrawableColorRadius = 0f
  private var mRightDrawableColorCircleEnable = false
  private var mRightDrawableWidth = 0
  private var mRightDrawableHeight = 0
  private var mRightDrawable: Drawable? = null
  private var mRightPressedDrawable: Drawable? = null
  private var mRightDisabledDrawable: Drawable? = null
  private var mRightSelectedDrawable: Drawable? = null
  private var mRightCheckedDrawable: Drawable? = null
  private var mBottomDrawableColorRadius = 0f
  private var mBottomDrawableColorCircleEnable = false
  private var mBottomDrawableWidth = 0
  private var mBottomDrawableHeight = 0
  private var mBottomDrawable: Drawable? = null
  private var mBottomPressedDrawable: Drawable? = null
  private var mBottomDisabledDrawable: Drawable? = null
  private var mBottomSelectedDrawable: Drawable? = null
  private var mBottomCheckedDrawable: Drawable? = null
  override fun initAttributes(context: Context?, attrs: AttributeSet?) {
    mTextView = mView as TextView?
    mTextColor = mTypedArray.getColor(R.styleable.RadiusSwitch_rv_textColor, Int.MAX_VALUE)
    mTextColor =
      if (mTextColor == Int.MAX_VALUE) mTextView!!.textColors.defaultColor else mTextColor
    mTextPressedColor =
      mTypedArray.getColor(R.styleable.RadiusSwitch_rv_textPressedColor, mTextColor)
    mTextDisabledColor =
      mTypedArray.getColor(R.styleable.RadiusSwitch_rv_textDisabledColor, mTextColor)
    mTextSelectedColor =
      mTypedArray.getColor(R.styleable.RadiusSwitch_rv_textSelectedColor, mTextColor)
    mTextCheckedColor =
      mTypedArray.getColor(R.styleable.RadiusSwitch_rv_textCheckedColor, mTextColor)
    mLeftDrawableColorRadius =
      mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_leftDrawableColorRadius, 0f)
    mLeftDrawableColorCircleEnable =
      mTypedArray.getBoolean(R.styleable.RadiusSwitch_rv_leftDrawableColorCircleEnable, false)
    mLeftDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_leftDrawableWidth, -1)
    mLeftDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_leftDrawableHeight, -1)
    mLeftDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_leftDrawable)
    mLeftPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_leftPressedDrawable)
    mLeftDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_leftDisabledDrawable)
    mLeftSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_leftSelectedDrawable)
    mLeftCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_leftCheckedDrawable)
    mTopDrawableColorRadius =
      mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_topDrawableColorRadius, 0f)
    mTopDrawableColorCircleEnable =
      mTypedArray.getBoolean(R.styleable.RadiusSwitch_rv_topDrawableColorCircleEnable, false)
    mTopDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_topDrawableWidth, -1)
    mTopDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_topDrawableHeight, -1)
    mTopDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_topDrawable)
    mTopPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_topPressedDrawable)
    mTopDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_topDisabledDrawable)
    mTopSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_topSelectedDrawable)
    mTopCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_topCheckedDrawable)
    mRightDrawableColorRadius =
      mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_rightDrawableColorRadius, 0f)
    mRightDrawableColorCircleEnable = mTypedArray.getBoolean(
      R.styleable.RadiusSwitch_rv_rightDrawableColorCircleEnable,
      false
    )
    mRightDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_rightDrawableWidth, -1)
    mRightDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_rightDrawableHeight, -1)
    mRightDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_rightDrawable)
    mRightPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_rightPressedDrawable)
    mRightDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_rightDisabledDrawable)
    mRightSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_rightSelectedDrawable)
    mRightCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_rightCheckedDrawable)
    mBottomDrawableColorRadius =
      mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_bottomDrawableColorRadius, 0f)
    mBottomDrawableColorCircleEnable = mTypedArray.getBoolean(
      R.styleable.RadiusSwitch_rv_bottomDrawableColorCircleEnable,
      false
    )
    mBottomDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_bottomDrawableWidth, -1)
    mBottomDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_bottomDrawableHeight, -1)
    mBottomDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_bottomDrawable)
    mBottomPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_bottomPressedDrawable)
    mBottomDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_bottomDisabledDrawable)
    mBottomSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_bottomSelectedDrawable)
    mBottomCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_bottomCheckedDrawable)
    super.initAttributes(context, attrs)
  }

  override fun init() {
    setTextSelector()
    setTextDrawable(
      mLeftDrawable, mLeftCheckedDrawable,
      mLeftSelectedDrawable, mLeftPressedDrawable, mLeftDisabledDrawable, Gravity.LEFT
    )
    setTextDrawable(
      mTopDrawable, mTopCheckedDrawable,
      mTopSelectedDrawable, mTopPressedDrawable, mTopDisabledDrawable, Gravity.TOP
    )
    setTextDrawable(
      mRightDrawable, mRightCheckedDrawable,
      mRightSelectedDrawable, mRightPressedDrawable, mRightDisabledDrawable, Gravity.RIGHT
    )
    setTextDrawable(
      mBottomDrawable, mBottomCheckedDrawable,
      mBottomSelectedDrawable, mBottomPressedDrawable, mBottomDisabledDrawable, Gravity.BOTTOM
    )
    super.init()
  }

  /**
   * 设置文本常态颜色
   *
   * @param color
   * @return
   */
  fun setTextColor(color: Int): RadiusTextViewDelegate {
    mTextColor = color
    return this
  }

  /**
   * 设置文本按下颜色
   *
   * @param color
   * @return
   */
  fun setTextPressedColor(color: Int): RadiusTextViewDelegate {
    mTextPressedColor = color
    return this
  }

  /**
   * 设置文本不可点击状态颜色--setEnable(false)时的颜色[View.setEnabled]
   *
   * @param color
   * @return
   */
  fun setTextDisabledColor(color: Int): RadiusTextViewDelegate {
    mTextDisabledColor = color
    return this
  }

  /**
   * 设置文本selected颜色
   *
   * @param color
   * @return
   */
  fun setTextSelectedColor(color: Int): RadiusTextViewDelegate {
    mTextSelectedColor = color
    return this
  }

  /**
   * 设置文本checked颜色
   *
   * @param color
   * @return
   */
  fun setTextCheckedColor(color: Int): RadiusTextViewDelegate {
    mTextCheckedColor = color
    return this
  }

  fun setLeftDrawableWidth(leftDrawableWidth: Int): RadiusTextViewDelegate {
    mLeftDrawableWidth = leftDrawableWidth
    return this
  }

  fun setLeftDrawableHeight(leftDrawableHeight: Int): RadiusTextViewDelegate {
    mLeftDrawableHeight = leftDrawableHeight
    return this
  }

  /**
   * 设置左边drawable
   *
   * @param drawable
   * @return
   */
  fun setLeftDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mLeftDrawable = drawable
    return this
  }

  fun setLeftDrawable(resId: Int): RadiusTextViewDelegate {
    return setLeftDrawable(getDrawable(resId))
  }

  fun setLeftPressedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mLeftPressedDrawable = drawable
    return this
  }

  fun setLeftPressedDrawable(resId: Int): RadiusTextViewDelegate {
    return setLeftPressedDrawable(getDrawable(resId))
  }

  fun setLeftDisabledDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mLeftDisabledDrawable = drawable
    return this
  }

  fun setLeftDisabledDrawable(resId: Int): RadiusTextViewDelegate {
    return setLeftDisabledDrawable(getDrawable(resId))
  }

  fun setLeftSelectedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mLeftSelectedDrawable = drawable
    return this
  }

  fun setLeftSelectedDrawable(resId: Int): RadiusTextViewDelegate {
    return setLeftSelectedDrawable(getDrawable(resId))
  }

  fun setLeftCheckedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mLeftCheckedDrawable = drawable
    return this
  }

  fun setLeftCheckedDrawable(resId: Int): RadiusTextViewDelegate {
    return setLeftCheckedDrawable(getDrawable(resId))
  }

  fun setTopDrawableWidth(topDrawableWidth: Int): RadiusTextViewDelegate {
    mTopDrawableWidth = topDrawableWidth
    return this
  }

  fun setTopDrawableHeight(topDrawableHeight: Int): RadiusTextViewDelegate {
    mTopDrawableHeight = topDrawableHeight
    return this
  }

  /**
   * 设置top drawable
   *
   * @param drawable
   * @return
   */
  fun setTopDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mTopDrawable = drawable
    return this
  }

  fun setTopDrawable(resId: Int): RadiusTextViewDelegate {
    return setTopDrawable(getDrawable(resId))
  }

  fun setTopPressedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mTopPressedDrawable = drawable
    return this
  }

  fun setTopPressedDrawable(resId: Int): RadiusTextViewDelegate {
    return setTopPressedDrawable(getDrawable(resId))
  }

  fun setTopDisabledDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mTopDisabledDrawable = drawable
    return this
  }

  fun setTopDisabledDrawable(resId: Int): RadiusTextViewDelegate {
    return setTopDisabledDrawable(getDrawable(resId))
  }

  fun setTopSelectedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mTopSelectedDrawable = drawable
    return this
  }

  fun setTopSelectedDrawable(resId: Int): RadiusTextViewDelegate {
    return setTopSelectedDrawable(getDrawable(resId))
  }

  fun setTopCheckedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mTopCheckedDrawable = drawable
    return this
  }

  fun setTopCheckedDrawable(resId: Int): RadiusTextViewDelegate {
    return setTopCheckedDrawable(getDrawable(resId))
  }

  fun setRightDrawableWidth(rightDrawableWidth: Int): RadiusTextViewDelegate {
    mRightDrawableWidth = rightDrawableWidth
    return this
  }

  fun setRightDrawableHeight(rightDrawableHeight: Int): RadiusTextViewDelegate {
    mRightDrawableHeight = rightDrawableHeight
    return this
  }

  /**
   * 设置right drawable
   *
   * @param drawable
   * @return
   */
  fun setRightDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mRightDrawable = drawable
    return this
  }

  fun setRightDrawable(resId: Int): RadiusTextViewDelegate {
    return setRightDrawable(getDrawable(resId))
  }

  fun setRightPressedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mRightPressedDrawable = drawable
    return this
  }

  fun setRightPressedDrawable(resId: Int): RadiusTextViewDelegate {
    return setRightPressedDrawable(getDrawable(resId))
  }

  fun setRightDisabledDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mRightDisabledDrawable = drawable
    return this
  }

  fun setRightDisabledDrawable(resId: Int): RadiusTextViewDelegate {
    return setRightDisabledDrawable(getDrawable(resId))
  }

  fun setRightSelectedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mRightSelectedDrawable = drawable
    return this
  }

  fun setRightSelectedDrawable(resId: Int): RadiusTextViewDelegate {
    return setRightSelectedDrawable(getDrawable(resId))
  }

  fun setRightCheckedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mRightCheckedDrawable = drawable
    return this
  }

  fun setRightCheckedDrawable(resId: Int): RadiusTextViewDelegate {
    return setRightCheckedDrawable(getDrawable(resId))
  }

  fun setBottomDrawableWidth(bottomDrawableWidth: Int): RadiusTextViewDelegate {
    mBottomDrawableWidth = bottomDrawableWidth
    return this
  }

  fun setBottomDrawableHeight(bottomDrawableHeight: Int): RadiusTextViewDelegate {
    mBottomDrawableHeight = bottomDrawableHeight
    return this
  }

  /**
   * 设置bottom drawable
   *
   * @param drawable
   * @return
   */
  fun setBottomDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mBottomDrawable = drawable
    return this
  }

  fun setBottomDrawable(resId: Int): RadiusTextViewDelegate {
    return setBottomDrawable(getDrawable(resId))
  }

  fun setBottomPressedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mBottomPressedDrawable = drawable
    return this
  }

  fun setBottomPressedDrawable(resId: Int): RadiusTextViewDelegate {
    return setBottomPressedDrawable(getDrawable(resId))
  }

  fun setBottomDisabledDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mBottomDisabledDrawable = drawable
    return this
  }

  fun setBottomDisabledDrawable(resId: Int): RadiusTextViewDelegate {
    return setBottomDisabledDrawable(getDrawable(resId))
  }

  fun setBottomSelectedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mBottomSelectedDrawable = drawable
    return this
  }

  fun setBottomSelectedDrawable(resId: Int): RadiusTextViewDelegate {
    return setBottomSelectedDrawable(getDrawable(resId))
  }

  fun setBottomCheckedDrawable(drawable: Drawable?): RadiusTextViewDelegate {
    mBottomCheckedDrawable = drawable
    return this
  }

  fun setBottomCheckedDrawable(resId: Int): RadiusTextViewDelegate {
    return setBottomCheckedDrawable(getDrawable(resId))
  }

  /**
   * 设置TextView的Left、Top、Right、Bottom Drawable属性
   *
   * @param normal
   * @param checked
   * @param selected
   * @param pressed
   * @param disabled
   * @param gravity
   */
  private fun setTextDrawable(
      normal: Drawable?, checked: Drawable?, selected: Drawable?, pressed: Drawable?,
      disabled: Drawable?, gravity: Int,
  ) {
    if (normal != null || pressed != null || disabled != null || selected != null || checked != null) {
      var index = 0
      var width = mLeftDrawableWidth
      var height = mLeftDrawableHeight
      var radius =
        if (mLeftDrawableColorCircleEnable) (width + height / 2).toFloat() else mLeftDrawableColorRadius
      when (gravity) {
        Gravity.TOP -> {
          index = 1
          width = mTopDrawableWidth
          height = mTopDrawableHeight
          radius =
            if (mTopDrawableColorCircleEnable) (width + height / 2).toFloat() else mTopDrawableColorRadius
        }

        Gravity.RIGHT -> {
          index = 2
          width = mRightDrawableWidth
          height = mRightDrawableHeight
          radius =
            if (mRightDrawableColorCircleEnable) (width + height / 2).toFloat() else mRightDrawableColorRadius
        }

        Gravity.BOTTOM -> {
          index = 3
          width = mBottomDrawableWidth
          height = mBottomDrawableHeight
          radius =
            if (mBottomDrawableColorCircleEnable) (width + height / 2).toFloat() else mBottomDrawableColorRadius
        }
      }
      val stateDrawable = StateListDrawable()
      stateDrawable.addState(
        intArrayOf(mStateChecked),
        getStateDrawable(checked, radius, width, height)
      )
      stateDrawable.addState(
        intArrayOf(mStateSelected),
        getStateDrawable(selected, radius, width, height)
      )
      stateDrawable.addState(
        intArrayOf(mStatePressed),
        getStateDrawable(pressed, radius, width, height)
      )
      stateDrawable.addState(
        intArrayOf(mStateDisabled),
        getStateDrawable(disabled, radius, width, height)
      )
      stateDrawable.addState(intArrayOf(), getStateDrawable(normal, radius, width, height))
      DrawableUtil.setDrawableWidthHeight(stateDrawable, width, height)
      val drawables = mTextView!!.compoundDrawables
      drawables[index] = stateDrawable
      mTextView!!.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3])
    }
  }

  /**
   * 获取不同状态下的Drawable信息
   *
   * @param drawable
   * @param radius
   * @param width
   * @param height
   * @return
   */
  protected fun getStateDrawable(
      drawable: Drawable?,
      radius: Float,
      width: Int,
      height: Int,
  ): Drawable? {
    var drawable = drawable
    if (drawable is ColorDrawable) {
      val gradientDrawable = GradientDrawable()
      gradientDrawable.setColor(drawable.color)
      gradientDrawable.cornerRadius = radius
      gradientDrawable.setSize(width, height)
      drawable = gradientDrawable
    }
    DrawableUtil.setDrawableWidthHeight(drawable, width, height)
    return drawable
  }

  /**
   * 设置文本颜色
   */
  private fun setTextSelector() {
    mTextView = mView as TextView?
    mTextColor =
      if (mTextColor == Int.MAX_VALUE)
        mTextView!!.textColors.defaultColor
      else mTextColor
    if (mTextColor != Int.MAX_VALUE
      || mTextPressedColor != Int.MAX_VALUE
      || mTextDisabledColor != Int.MAX_VALUE
      || mTextSelectedColor != Int.MAX_VALUE
      || mTextCheckedColor != Int.MAX_VALUE
    ) {
      val colorStateList = getColorSelector(
        mTextColor,
        mTextPressedColor,
        mTextDisabledColor,
        mTextSelectedColor,
        mTextCheckedColor
      )
      mTextView!!.setTextColor(colorStateList)
    }
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private fun getColorSelector(
      normalColor: Int,
      pressedColor: Int,
      DisabledColor: Int,
      selectedColor: Int,
      checkedColor: Int,
  ): ColorStateList {
    return ColorStateList(
      arrayOf(
        intArrayOf(mStateChecked),
        intArrayOf(mStateSelected),
        intArrayOf(mStatePressed),
        intArrayOf(mStateDisabled),
        intArrayOf()
      ),
      intArrayOf(
        checkedColor,
        selectedColor,
        pressedColor,
        DisabledColor,
        normalColor
      )
    )
  }

  /**
   * 根据drawable资源id返回drawable对象
   *
   * @param resId
   * @return
   */
  protected fun getDrawable(resId: Int): Drawable? {
    val drawable: Drawable? = null
    try {
      mView!!.resources.getDrawable(resId)
    } catch (e: Exception) {
    }
    return drawable
  }
}